package icu.honeysuccess.file.controller;

import icu.honeysuccess.entity.Result;
import icu.honeysuccess.entity.StatusCode;
import icu.honeysuccess.file.util.FastDFSClient;
import icu.honeysuccess.file.util.FastDFSFile;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author Honey
 * @date 2022/3/17
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile file) {
        try {
            if (file == null) {
                throw new RuntimeException("文件不存在！");
            }
            // 准备FastDFSFile对象属性
            String filename = file.getOriginalFilename();
            if (StringUtils.isEmpty(filename)) {
                throw new RuntimeException("文件不存在！");
            }
            String extname = filename.substring(filename.lastIndexOf(".") + 1);
            byte[] bytes = file.getBytes();
            // 创建FastDFSFile对象
            FastDFSFile dfsFile = new FastDFSFile(filename,bytes,extname);
            String[] upload = FastDFSClient.upload(dfsFile);
            for (int i = 0; i < upload.length; i++) {
                System.out.println(upload[i]);
            }
            String url = FastDFSClient.getTrackerUrl() + upload[0] + "/" + upload[1];
            return new Result(true, StatusCode.OK, "上传成功！", url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,StatusCode.ERROR,"上传失败！");
    }




    @DeleteMapping("/delete")
    public Result delete(@RequestParam String groupName, @RequestParam String remoteFileName) {
        try {
            FastDFSClient.deleteFile(groupName,remoteFileName);
            return new Result(true,StatusCode.OK,"删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,StatusCode.ERROR,"删除失败！");
    }

    @GetMapping("/download")
    public void download(@RequestParam String groupName,
                         @RequestParam String remoteFileName,
                         @RequestParam String path){
        InputStream inputStream = FastDFSClient.downFile(groupName, remoteFileName);
        // 创建一个文件数据流
        BufferedInputStream bis = new BufferedInputStream(inputStream);

        File file = new File(path);
        // 不存在该目录，去创建该目录
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        // 把文件存在服务器的文件夹里
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            int size = 0;
            byte[] buf = new byte[1024];
            while ((size = bis.read(buf)) != -1) {
                out.write(buf, 0, size);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        
    }

}